package org.abstractmeta.commandify.core.command.reflectify;

import org.abstractmeta.commandify.core.command.AbstractFieldCommand;
import org.abstractmeta.reflectify.Mutator;
import org.abstractmeta.reflectify.Reflectify;

/**
 * Reflectify field command
 */
public class ReflectifyFieldCommand<T, V> extends AbstractFieldCommand<T, V> {

    private final Mutator<T, V> mutator;

    @SuppressWarnings("unchecked")
    public ReflectifyFieldCommand(Reflectify<T> reflectify, String source, V value, V previousValue) {
        super(source, value, previousValue);
        this.mutator= Mutator.class.cast(reflectify.getMutator(source));
    }

    @Override
    public void execute(T instance) {
        V clonedValue = getCommandValue(getValue());
        mutator.set(instance, clonedValue);
    }

}
